import { MES_SMH_ApiHost } from '@/utils/Interceptors'

// 任务数据类型 - 根据Swagger文档
export interface Task {
  taskId?: number
  orderNo?: string
  defectItemId?: number
  taskName?: string
  taskCode?: string
  workPermission?: string
  reportQtyRatio?: number
  reportTime?: string
  defectiveItemList?: string
  allocationList?: string
  plannedStartTime?: string
  plannedEndTime?: string
  plannedQty?: number
  goodQty?: number
  badQty?: number
  actualStartTime?: string
  actualEndTime?: string
  creator?: string
  createTime?: string
  modifier?: string
  modifyTime?: string
}

// API响应类型
export interface ApiResponse<T> {
  code: number
  msg: string
  data: T
}

// 分页响应类型
export interface PagedResponse<T> {
  tasks: T[]
  totalCount: number
  totalPages: number
  currentPage: number
  pageSize: number
}

// 查询参数类型
export interface TaskQueryParams {
  taskCode?: string
  orderNo?: string
  pageIndex: number
  pageSize: number
}

// 高级查询参数类型
export interface TaskAdvancedQueryParams {
  orderNo?: string
  taskName?: string
  taskCode?: string
  defectiveItemList?: string
  plannedQty?: number
  goodQty?: number
  badQty?: number
  plannedStartTimeFrom?: string
  plannedStartTimeTo?: string
  plannedEndTimeFrom?: string
  plannedEndTimeTo?: string
  actualStartTimeFrom?: string
  actualStartTimeTo?: string
  actualEndTimeFrom?: string
  actualEndTimeTo?: string
  createTimeFrom?: string
  createTimeTo?: string
  pageIndex: number
  pageSize: number
}

// 获取任务分页列表
export const getTaskPageList = async (params: TaskQueryParams) => {
  return await MES_SMH_ApiHost.post<ApiResponse<PagedResponse<Task>>>('/api/Task/GetPageList', params)
}

// 获取任务高级查询分页列表
export const getTaskAdvancedPageList = async (params: TaskAdvancedQueryParams) => {
  return await MES_SMH_ApiHost.post<ApiResponse<PagedResponse<Task>>>('/api/Task/GetPageList', params)
}

// 创建任务
export const createTask = async (data: Partial<Task>) => {
  return await MES_SMH_ApiHost.post<ApiResponse<Task>>('/api/Task/Create', data)
}

// 更新任务
export const updateTask = async (id: number, data: Partial<Task>) => {
  return await MES_SMH_ApiHost.put<ApiResponse<Task>>(`/api/Task/Update/${id}`, data)
}

// 删除任务
export const deleteTask = async (id: number) => {
  return await MES_SMH_ApiHost.delete<ApiResponse<boolean>>(`/api/Task/Delete/${id}`)
}

// 批量删除任务
export const batchDeleteTasks = async (ids: number[]) => {
  return await MES_SMH_ApiHost.post<ApiResponse<boolean>>('/api/Task/BatchDelete', { ids })
}
